# -*- coding: utf-8 -*-

"""
@Author: 芮芪懿 QQ1849433491
@Project->File: pygame_train -> 4绘制攻击飞机.py
@IDE：PyCharm 2020.1
@File: 4绘制攻击飞机.py
@Datetime: 2021/7/7 21:20
"""
import time
import pygame
from resource_path import *


def update_screen():
    """
    update 更新屏幕显示背景图片
    :return:
    """
    pygame.display.update()
    pass


def load_image_resource(path=""):
    """
    加载图片资源
    :param path: 图片路径
    :return: 图片资源
    """
    return pygame.image.load(path)
    pass


pygame.init()
bgImage = pygame.image.load(bgImagePath)  # 加载背景图像数据
player1Image = load_image_resource(player1ImagePath)  # 加载背景图像数据
player2Image = load_image_resource(player2ImagePath)  # 加载背景图像数据
bgImageRect = bgImage.get_rect()  # 获取背景矩形
bgImageWidth = bgImageRect.width  # 获取图片宽度
bgImageHeight = bgImageRect.height  # 获取图片高度
screen = pygame.display.set_mode((bgImageWidth, bgImageHeight))  # 屏幕对象,宽高素材一致
# blit 绘制图像
screen.blit(bgImage, (0, 0))
screen.blit(bgImage, (0, -bgImageHeight))
update_screen()
imageYStartPosition = 0  # 背景 y轴初始位置
imageYStartPosition2 = - bgImageHeight  # 背景2 y轴初始位置
bgImageOffsetY = 4  # 图片每次移动的Y值
framerate = 90  # 帧数
clock = pygame.time.Clock()  # 创建时钟对象


def set_bgImageOffsetYCycle():
    """
    设置动态背景
    :return:
    """
    global imageYStartPosition
    global imageYStartPosition2
    # 增加Y的偏移量,并重新绘制图片
    imageYStartPosition += bgImageOffsetY
    imageYStartPosition2 += bgImageOffsetY
    screen.blit(bgImage, (0, imageYStartPosition))
    screen.blit(bgImage, (0, imageYStartPosition2))
    # 判断偏移量是否超出了背景图片的高度,是->重置偏移量
    if imageYStartPosition >= bgImageHeight or imageYStartPosition2 >= bgImageHeight:
        imageYStartPosition = 0
        imageYStartPosition2 = - bgImageHeight
    pass


def setPlayerMoveCycle():
    # global bgImageRect
    # global bgImageHeight
    # global player1Image
    # global player2Image
    playerRect = player1Image.get_rect()  # 玩家飞机矩形
    playerWidth = playerRect.width
    playerHeight = playerRect.height
    i = 0
    # while True:
    #     """
    #     游戏动态背景循环
    #     """
    screen.blit(player1Image, (bgImageRect.centerx - (playerWidth / 2), bgImageHeight - 200))
    update_screen()
    clock.tick(60)
    screen.blit(player2Image, (bgImageRect.centerx - (playerWidth / 2), bgImageHeight - 200))
    pass


while True:
    """
    游戏动态背景循环
    """
    clock.tick(90)  # 设置帧数
    set_bgImageOffsetYCycle()
    setPlayerMoveCycle()
    update_screen()
    pass

pygame.quit()